<template>
  <div class="page_content">
    <header>
      <van-nav-bar class="main_header" title="设置">
        <template #left>
          <span @click="goBack">返回</span>
        </template>
      </van-nav-bar>
    </header>
    <div class="main_content">
      <van-cell v-for="item in optMenuList" :key="item.code" :title="item.name" is-link @click="handleClickMenu(item)" />
      <div class="logout gl_mx_15 gl_mt_30">
        <van-button type="danger" block @click="comfirmLogout()">退出登录</van-button>
      </div>
    </div>
    <update-password ref="updatePassword" />
  </div>
</template>
<script>
import UpdatePassword from './updatePassword';
import { judgeToLogout } from '@/lib/oidc_fun';
export default {
  components: {
    UpdatePassword
  },
  data() {
    return {
      optMenuList: [
        {
          code: 'updatePassword',
          name: '密码修改',
          role: ''
        }
      ]
    };
  },
  computed: {
    tenantId() {
      let tenantId = this.gameId
        ? this.gameId.lastIndexOf('_') > -1
          ? this.gameId.substring(this.gameId.lastIndexOf('_') + 1)
          : ''
        : '';
      tenantId = tenantId || this.$route.query.tenantid;
      return tenantId;
    }
  },
  methods: {
    handleClickMenu(menuItem) {
      switch (menuItem.code) {
        case 'updatePassword':
          this.$refs.updatePassword.open();
          break;
        default:
          break;
      }
    },
    // 退出登录
    comfirmLogout() {
      this.$dialog.confirm({
        title: '提示',
        message: '您确定退出吗?'
      }).then(() => {
        this.logout();
      }).catch(() => {});
    },
    async logout() {
      const tenantId = !(
        this.$route.params &&
        this.$route.params.id &&
        this.$route.params.id.lastIndexOf('_') > -1
      )
        ? this.$store.state.user.info && this.$store.state.user.info.tenantId
          ? this.$store.state.user.info.tenantId
          : ''
        : this.tenantId || '';
      await this.$store.dispatch('user/logout');
      let refs =
        window.location.href.indexOf('/myCenter') > -1
          ? window.location.origin + window.location.pathname + `#/home${this.$route.params.id && '/' + this.$route.params.id + '/info' || ''}`
          : window.location.href;
      refs =
        refs.indexOf('#') > -1
          ? refs.indexOf('?') > -1
            ? refs + '&'
            : refs + '?'
          : refs + '#/?';
      refs = refs
        .replace(/tenantid=[0-9a-z\-]+|tenantid=/g, '')
        .replace(/&&+/g, '&')
        .replace('?&', '?');
      judgeToLogout(refs + 'tenantid=' + tenantId);
    },
    goBack() {
      this.$router.push({
        path: `/myCenter${this.$route.params.id && '/' + this.$route.params.id || ''}/`
      });
    }
  }
};
</script>
